<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\JsonResponse;

class Logging
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure                 $next
     *
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        /* @var $response JsonResponse */
        $response = $next($request);

        // 记录日志的信息
        $data = [
            'host_name'        => $request->getHttpHost(),
            'ip'               => get_ip(),
            'method'           => $request->getMethod(),
            'request_uri'      => $request->url(),
            'request_params'   => $request->all(),
            'request_time'     => date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']),
            'request_duration' => (microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']),
            'response_time'    => date('Y-m-d H:i:s'),
            'response_body'    => $response->getData(true),
        ];

        logger()->info('logging', $data);
        return $response;
    }
}
